# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Sertonix <sertonix@posteo.net>
pkgname=syncthing
pkgver=1.27.7
pkgrel=0
pkgdesc="Open Source Continuous File Synchronization"
url="https://syncthing.net/"
arch="all"
license="MPL-2.0"
pkgusers="$pkgname"
pkggroups="$pkgname"
makedepends="go"
install="$pkgname.pre-install"
subpackages="$pkgname-doc $pkgname-utils $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/syncthing/syncthing/archive/v$pkgver.tar.gz
	build-unset-CGO_ENABLED.patch
	only-test-with-race-when-provided.patch
	$pkgname.initd
	"
options="chmod-clean net"
case "$CARCH" in
# arm*: oom on tests
# riscv64: bind error
arm*|riscv64) options="$options !check" ;;
esac

# secfixes:
#   1.15.1-r0:
#     - CVE-2021-21404

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	# Build syncthing + server utils
	local cmd
	for cmd in syncthing stdiscosrv strelaypoolsrv strelaysrv; do
		echo "Compiling $cmd"
		go run build.go -no-upgrade -version=v$pkgver install $cmd
	done
}

check() {
	# shellcheck disable=2046
	# very flake
	go test $(go list ./lib/... ./cmd/... | grep -Ev '(lib/model)')
}

package() {
	local i

	install -d -o $pkgname -g $pkgname "$pkgdir"/var/lib/$pkgname
	install -D -m755 -t "$pkgdir"/usr/bin/ "$builddir"/bin/*
	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	# Desktop files so people in Desktop Environments can put them on Autostart
	install -Dm0644 etc/linux-desktop/*.desktop -t "$pkgdir"/usr/share/applications

	for i in 32 64 128 256 512; do
		install -Dm644 "$builddir"/assets/logo-"$i".png \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done
	install -Dm644 "$builddir"/assets/logo-only.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

	# man pages
	for i in 1 5 7; do
		install -Dm644 -t "$pkgdir"/usr/share/man/man"$i"/ "$builddir"/man/*."$i"
	done
}

utils() {
	pkgdesc="Syncthing server utilities"

	amove 'usr/bin/st*'
}

sha512sums="
43207cf203c40a9846b50789c6e45f2c2bfbcf14cdcdc735b076520343506f20556f4609ab456e53064c0015555bc764ed7ce564b3e5b24f657fd50f55c8f05b  syncthing-1.27.7.tar.gz
30c96641f073ba732d495e9df4b04a9f00ddb19439b4764c2aef07e4ec4a5f53ec6527210c76dc2d2af8f76a37666e44c57acd444206427a258c1b39b53ee5d0  build-unset-CGO_ENABLED.patch
81bcb6b2e0956624b596201d1de24a3b6fcb10d08761f2c426081350b611295a7f4d47775d175f2ee5dbbb289b98bc022389fc9992f0d31bcdbfde855ceafaf8  only-test-with-race-when-provided.patch
8c95d8794e10c97d6546a48b07af769e9c1b26870fcc53d524868ca6e57e96ad686ebcd988ddb2f355404cced81b9f19eeba99a7c832eb4641ff0066f7916749  syncthing.initd
"
